package sshstudy.host.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import sshstudy.common.action.BaseAction;
import sshstudy.host.model.Host;
import sshstudy.host.service.HostService;


@Component
@Namespace("/host")
@ResultPath(value="/")
public final class HostAddAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;
	
	private Host host;
	
	@Autowired
	private HostService hostService;

	@Action(value="toAdd",
		results={@Result(name="success", location="add.jsp")}
	)
	public String toAdd() throws Exception {
		return SUCCESS;
	}
	
	@Action(value="doAdd",
		results={
			@Result(name="success", location="list.action", type="redirect"),
			@Result(name="input", location="add.jsp")
		}
	)
	public String doAdd() throws Exception {
		host.setId(String.valueOf(System.currentTimeMillis()));
		hostService.add(host);
		return SUCCESS;
	}

	public Host getHost() {
		return host;
	}

	public void setHost(Host host) {
		this.host = host;
	}

	public HostService getHostService() {
		return hostService;
	}

	public void setHostService(HostService hostService) {
		this.hostService = hostService;
	}
}
